﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class TasinmazYaz
    {
        public byte[] Yaz(List<Tasinmaz> TasinmazList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "Taşınmaz Listesi");

            int satir = 0;
            int sutun = 0;


            workBook.setText(satir, sutun, "TasınmazID");
            sutun++;
            workBook.setText(satir, sutun, "İl Adı");
            sutun++;
            workBook.setText(satir, sutun, "İlce Adı");
            sutun++;
            workBook.setText(satir, sutun, "Kurum Adı");
            sutun++;
            workBook.setText(satir, sutun, "Mahalle Adı");
            sutun++;
            workBook.setText(satir, sutun, "Pafta");
            sutun++;
            workBook.setText(satir, sutun, "Ada");
            sutun++;
            workBook.setText(satir, sutun, "Parsel");
            sutun++;
            workBook.setText(satir, sutun, "Tasinmaz Tipi");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            workBook.setText(satir, sutun, "Cilt");
            sutun++;
            workBook.setText(satir, sutun, "Sayfa");
            sutun++;
            workBook.setText(satir, sutun, "Parsel Tipi");
            sutun++;

            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (Tasinmaz Tasinmaz in TasinmazList)
            {
                satir++;
                sutun = 0;

                if (Tasinmaz.TasinmazID != null)
                    deger = Tasinmaz.TasinmazID.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Il != null)
                    deger = Tasinmaz.Il.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Ilce != null)
                    deger = Tasinmaz.Ilce.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Kurum != null)
                    deger = Tasinmaz.Kurum.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Mahalle != null)
                    deger = Tasinmaz.Mahalle.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Pafta != null)
                    deger = Tasinmaz.Pafta.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Ada != null)
                    deger = Tasinmaz.Ada.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Parsel != null)
                    deger = Tasinmaz.Parsel.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.TasinmazTip != null)
                    deger = Tasinmaz.TasinmazTip.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.TapuBolumDurum != null)
                    deger = Tasinmaz.TapuBolumDurum.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Cilt != null)
                    deger = Tasinmaz.Cilt.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.Sayfa != null)
                    deger = Tasinmaz.Sayfa.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (Tasinmaz.ParselTip != null)
                    deger = Tasinmaz.ParselTip.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;


                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun - 1);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}

